package com.ebelter.bpm.activitys;


import android.text.TextUtils;
import android.view.View;

import com.ebelter.bpm.*;

import com.ebelter.btcomlib.bases.activity.BaseActivity;
import com.ebelter.btcomlib.common.Constants;
import com.ebelter.btcomlib.models.beans.ScaleUser;
import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.models.https.responses.LoginResponse;
import com.ebelter.btcomlib.utils.CommonUtils;

import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.activity_manager.ActivityManagers;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.btcomlib.views.CombinView1;


import static com.ebelter.btcomlib.models.beans.ScaleUser.userid2btid;

public class BPM_LoginActivity extends BaseActivity {


    private static final String TAG = "BPM_LoginActivity";

    CombinView1 name_cb1;
    CombinView1 psw_cb1;

    @Override
    protected int loadLayoutById() {
        return R.layout.activity_bpm_login;
    }

    @Override
    protected void initView() {
        FV();

    }

    private void FV() {
        name_cb1=findViewById(R.id.cb1_bpm_login_nickname);
        psw_cb1=findViewById(R.id.cb1_bpm_login_p1);
    }

    //登录按钮
    public void bt_sign_bpm_login(View view){

        String nickName = name_cb1.getTitle1();
        String psw = psw_cb1.getTitle1();

        if (TextUtils.isEmpty(nickName)){
            ToastUtil.show(R.string.Nikename_cannot_be_empty);
            return;
        }
        if (TextUtils.isEmpty(psw)){
            ToastUtil.show(R.string.The_password_cannot_be_empty);
            return;
        }
        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            return;
        }



        NetUtils.getInstance().loginRequest(this, nickName,psw, new HttpResponse<LoginResponse>() {
            @Override
            public void result(boolean isRequestSuccess, String requestDes, LoginResponse loginResponse, String s) {
                if (!isRequestSuccess){
                    ToastUtil.show(R.string.Network_connection_error);
                    return;
                }
                if (loginResponse.resultCode==0){
                    //登录成功
                    saveUserDatas(loginResponse.getResultData());
                    startActivity(BPMMainActivity.class);
                    ActivityManagers.getInstance().closeActivity(BPM_SplashActivity.TAG);
                    finish();
                }else if (loginResponse.getResultCode()==109){//109是：不存在的email或者昵称
                    ToastUtil.show(R.string.mail_nickname_no_exist);
                }else if(loginResponse.getResultCode()==111){//111密码错误
                    ToastUtil.show(R.string.psw_error);
                }else if (loginResponse.getResultCode()==110){//服务器异常
                    ToastUtil.show(R.string.An_exception_occurred_on_the_server);
                }



            }
        });




    }

    //保存登录数据
    private void saveUserDatas(LoginResponse.ResultDataBean resultData) {

        UserSpUtil.removeAll();


        LogUtils.i(TAG,"----------"+resultData.toString());

        long useridlong = resultData.getUserId();
        UserSpUtil.writeLong(Constants.IUser.USER_ID,useridlong);

        // TODO: 2018/4/23 zcq
        String btId = ScaleUser.getUser().toBtId(userid2btid(useridlong));
        LogUtils.i(TAG, "-----loginSave--useridlong = " + useridlong);
        LogUtils.i(TAG, "-----loginSave--btId = " + btId);

        UserSpUtil.writeString(Constants.IUser.BT_ID,btId);
        UserSpUtil.writeString(Constants.IUser.NICK_NAME,resultData.getNickName());
        UserSpUtil.writeString(Constants.IUser.EMAIL,resultData.getEmail());
        UserSpUtil.writeString(Constants.IUser.PHONE,resultData.getMobilePhone());
        UserSpUtil.writeInt(Constants.IUser.HEIGHT,resultData.getHeight());
        UserSpUtil.writeFloat(Constants.IUser.WEIGHT,resultData.getWeight());

        String birthday=resultData.getBirthday();
        UserSpUtil.writeString(Constants.IUser.BIRTHDAY,birthday);
        int year= CommonUtils.getBirthdayIntValue(birthday,0,1990);
        UserSpUtil.writeInt(Constants.IUser.YEAR,year);
        UserSpUtil.writeInt(Constants.IUser.MONTHS,CommonUtils.getBirthdayIntValue(birthday,1,6));
        UserSpUtil.writeInt(Constants.IUser.DAY,CommonUtils.getBirthdayIntValue(birthday,2,1));
        UserSpUtil.writeInt(Constants.IUser.AGE,CommonUtils.getAgeByBirthday(year));

        UserSpUtil.writeInt(Constants.IUser.SEX,resultData.getSex());
        UserSpUtil.writeInt(Constants.IUser.PROFESSION,resultData.getProfession());
        UserSpUtil.writeFloat(Constants.IUser.IMPEDANCE,resultData.getImpedance());
        UserSpUtil.writeString(Constants.IUser.CREATEDATE,resultData.getCreateDate());
        UserSpUtil.writeString(Constants.IUser.UPDATEDATE,resultData.getUpdateDate());
        UserSpUtil.writeFloat(Constants.IUser.TARGET_WEIGHT,resultData.getTargetWeight());








    }

    //注册按钮
    public void goToResgister(View view){
        startActivity(BPM_RegisterActivity.class);
    }
    //忘记密码
    public void tv_forgot_password(View view){
        startActivity(BPM_VerificEmailCodeActivity.class);
    }
}
